//
//  HorizontalLayout.swift
//  clreader
//
//  Created by colin on 2019/3/11.
//  Copyright © 2019 colin. All rights reserved.
//

import UIKit

// 横向布局
class HorizontalLayout: UICollectionViewLayout {
    private var isShowFooter:Bool = false
    private var footerWidth:CGFloat = 0
    
    private var padding:UIEdgeInsets = UIEdgeInsets.zero
    private var horizontalColumnSpace:CGFloat = 0.0
    
    private var attrsArray = [UICollectionViewLayoutAttributes]()
    
    //MARK:回调
    var itemSize:(() -> CGSize)?
    
    init(horizontalColumnSpace:CGFloat = 0.0,
         padding:UIEdgeInsets = UIEdgeInsets.zero,
         footerWidth:CGFloat = 64.0,
         isShowFooter:Bool = false,
         itemSize:(() -> CGSize)?) {
        super.init()
        self.horizontalColumnSpace = horizontalColumnSpace
        self.footerWidth = footerWidth
        self.isShowFooter = isShowFooter
        self.padding = padding
        self.itemSize = itemSize
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func prepare() {
        super.prepare()
        
        self.attrsArray.removeAll()
        
        if let count = collectionView?.numberOfItems(inSection: 0) {
            for index in 0 ..< count {
                let indexPath = IndexPath(item: index, section: 0)
                if let attrs = layoutAttributesForItem(at: indexPath) {
                    self.attrsArray.append(attrs)
                }
            }
            
            if self.isShowFooter {
                if let attrs = layoutAttributesForSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, at: IndexPath(item:0,section: 0)) {
                    self.attrsArray.append(attrs)
                }
            }
        }
    }
    
    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        return self.attrsArray
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        // 创建布局属性
        let attrs = UICollectionViewLayoutAttributes(forCellWith: indexPath)
        
        if let size = itemSize?() {
            let index = indexPath.item
            let x = (size.width + self.horizontalColumnSpace) * CGFloat(index) + self.padding.left
            let y = self.padding.top
            
            attrs.frame = CGRect(x: x, y: y, width: size.width, height: size.height)
        }
        return attrs
    }
    
    override var collectionViewContentSize : CGSize {
        
        var height = padding.top + padding.bottom
        if let height_ = itemSize?().height {
            height += floor(height_)
        }
        
        var width = padding.left + padding.right
        if let count = collectionView?.numberOfItems(inSection: 0) , let width_ = itemSize?().width {
            width += CGFloat(count) * (width_ + horizontalColumnSpace) - horizontalColumnSpace
        }
        
        return CGSize(width: width, height: height)
    }
    
    override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        
        // 创建布局属性
        let attrs = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: elementKind, with: indexPath)
        if let itemHeight = itemSize?().height , elementKind == UICollectionView.elementKindSectionFooter,
            let collectionViewWidth = collectionView?.frame.size.width {
            if collectionViewContentSize.width >= collectionViewWidth {
                attrs.frame = CGRect(x: collectionViewContentSize.width, y: padding.top, width: footerWidth, height: itemHeight)
                attrs.alpha = 1
            }
            else {
                attrs.alpha = 0
            }
        }
        
        return attrs
    }
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
}
